﻿using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class CFExNonCF12
    {
        private CFComparisonFunction _cp;
        private ushort _icf;
        private byte cbTemplateParm;
        internal DXFN12 dxf;
        internal bool fActive;
        internal bool fHasDXF;
        internal bool fStopIfTrue;
        private byte icfTemplate;
        internal ushort ipriority;
        private bool reserved1;
        private CFExTemplateParams rgbTemplateParms;

        public CFExNonCF12(CFExTemplateParams templateParameters, BiffWriterWorkbookContext context,
                           StandardConditionalFormattingRule rule)
        {
            rgbTemplateParms = templateParameters;
            cbTemplateParm = templateParameters.Length;
            icfTemplate = templateParameters.Type;
            dxf.SetFromDifferencialStyleRecord(rule.DifferentialStyleRecord, context);
            ipriority = (ushort) rule.Priority;
            _cp = CFRecord.GetComparisonFunction(rule);
        }

        public CFExNonCF12()
        {
        }

        internal byte Ae
        {
            get
            {
                byte result = 0;
                if (fActive)
                    result |= 0x01;
                if (fStopIfTrue)
                    result |= 0x02;
                if (reserved1)
                    result |= 0x04;
                return result;
            }
        }

        public CFExTemplateParams RgbTemplateParms
        {
            get { return rgbTemplateParms; }
        }


        public void Read(BinaryReader reader)
        {
#if DEBUG
            ReadWriteChecker.CheckClass((t, r) => t.Read(r),
                                        (t, w) => t.Write(w), reader, this);
#endif
            _icf = reader.ReadUInt16();
            _cp = (CFComparisonFunction) reader.ReadByte();
            icfTemplate = reader.ReadByte();
            ipriority = reader.ReadUInt16();
            byte ae = reader.ReadByte();
            fActive = (ae & 0x01) != 0;
            fStopIfTrue = (ae & 0x02) != 0;
            reserved1 = (ae & 0x04) != 0;
            fActive = (ae & 0x01) != 0;
            Contract.Assert(ae == Ae);
            fHasDXF = reader.ReadByte() != 0;

            if (fHasDXF)
            {
                dxf = new DXFN12();
                dxf.Read(reader);
            }
            cbTemplateParm = reader.ReadByte();

            rgbTemplateParms = CFExTemplateParams.Create((CFTemplate) icfTemplate);
            rgbTemplateParms.Read(reader);
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(_icf);
            writer.Write((byte) _cp);
            writer.Write(icfTemplate);
            writer.Write(ipriority);
            writer.Write(Ae);
            if (fHasDXF)
            {
                writer.Write((byte)1);
                dxf.Write(writer);
            }
            else
            {
                writer.Write((byte)0);
            }
            writer.Write(cbTemplateParm);
            rgbTemplateParms.Write(writer);
        }
    }
}